package org.mashibing.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import org.mashibing.constant.CommonStatusEnum;
import org.mashibing.constant.IdentityConstant;
import org.mashibing.constant.TokenTypeConstant;
import org.mashibing.dto.ResponseResult;
import org.mashibing.remote.ServicePassengerUserClient;
import org.mashibing.remote.ServiceVerificationClient;
import org.mashibing.request.VerificationCodeDTO;
import org.mashibing.response.NumberCodeResponse;
import org.mashibing.response.TokenResponse;
import org.mashibing.service.VerificationCodeService;
import org.mashibing.util.RedisPrefixUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.mashibing.util.JwtUtils;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author:
 * @create: 2024/3/20 10:27
 * @description:
 */
@Service
public class VerificationCodeServiceImpl implements VerificationCodeService {

    @Autowired
    private ServiceVerificationClient serviceVerificationClient;
    @Autowired
    private ServicePassengerUserClient servicePassengerUserClient;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    @Override
    public ResponseResult<NumberCodeResponse> generatorCode(String passenger,int size) {
        ResponseResult<NumberCodeResponse> numberCode = serviceVerificationClient.getNumberCode(size);
        // 拿到验证码
        Integer code = numberCode.getData().getNumberCode();
        // 存入redis
        String key = RedisPrefixUtil.generatorKeyByPhone(passenger, IdentityConstant.PASSENGER_IDENTITY);
        stringRedisTemplate.opsForValue().set(key,code+"",2, TimeUnit.MINUTES);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult<TokenResponse> checkCode(String passengerPhone, String verificationCode) {
        // 根据手机号在redis获取验证码
        String codeRedis = stringRedisTemplate.opsForValue().get(RedisPrefixUtil.generatorKeyByPhone(passengerPhone, IdentityConstant.PASSENGER_IDENTITY));
        // 校验验证码
        if (StringUtils.isBlank(codeRedis)) {
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        if (!Objects.equals(verificationCode.trim(),codeRedis.trim())) {
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        // 判断系统是否有该用户，并相应处理
        servicePassengerUserClient.loginOrRegister(new VerificationCodeDTO(passengerPhone,verificationCode,null));
        // 颁发令牌
        String accessToken = JwtUtils.generatorToken(passengerPhone, IdentityConstant.PASSENGER_IDENTITY, TokenTypeConstant.ACCESS_TOKEN_TYPE);
        String refreshToken = JwtUtils.generatorToken(passengerPhone, IdentityConstant.PASSENGER_IDENTITY,TokenTypeConstant.REFRESH_TOKEN_TYPE);

        // token存入redis
        String accessTokenKey = RedisPrefixUtil.generatorTokenKey(passengerPhone, IdentityConstant.PASSENGER_IDENTITY, TokenTypeConstant.ACCESS_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(accessTokenKey,accessToken,30,TimeUnit.DAYS);
        String refreshTokenKey = RedisPrefixUtil.generatorTokenKey(passengerPhone, IdentityConstant.PASSENGER_IDENTITY, TokenTypeConstant.REFRESH_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,31,TimeUnit.DAYS);


        return ResponseResult.success(new TokenResponse(accessToken,refreshToken));
    }


}
